﻿using System;
using Avalonia;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading;
using TCP232.ViewModels;
using TCP232.Views;

namespace TCP232.PublicMethod;

public static class MessageTip
{
    public static ButtonResult Show(string message, string title = null)
    {
        return ShowWindow(message, "info", title);
    }

    public static ButtonResult ShowWarning(string message, string title = "Warning")
    {
        return ShowWindow(message, "warning", title);
    }

    public static ButtonResult ShowError(string message, string title = "Error")
    {
        return ShowWindow(message, "error", title);
    }

    static ButtonResult ShowWindow(string message, string url, string title)
    {
        ButtonResult buttonResult = ButtonResult.None;
        Dispatcher.UIThread.Post(async () =>
        {
            MessageWindow messageWindow = new MessageWindow();
            if (!string.IsNullOrEmpty(title))
            {
                messageWindow.Title = title;
            }
            messageWindow.txtMessage.Text = message;
            var assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
            messageWindow.image.Source = new Bitmap(assets.Open(new Uri($"avares://TCP232/Assets/Image/{url}.png")));
            await messageWindow.ShowDialog(MainWindowViewModel.Instance.MainWindow);
            buttonResult = messageWindow.ButtonResult;
        });
        return buttonResult;
    }
}